package com.example.jiemian;

import java.util.ArrayList;
import java.util.List;

import com.demo.fenye.Controller;
import com.demo.fenye.ImageAdapter;
import com.example.jiemian.R;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.ScaleAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Fenye_Activity extends Activity {
	private RelativeLayout fenye_activity_bj;
	private ImageView liulan_btu, jiaye_btu, back_btu, fenye_edit_btu;
	private Gallery fenye_GALLERY;
	private ImageAdapter adapter;
	private ViewGroup group;
	//private ImageView[] tips;
	private ArrayList<ImageView> tips;
	private ArrayList<ImageView> tips1;
	private int oldPosition = 0;// 记录上一次点的位�??
	private int oldPosition_d = 0;// 记录上一次点的位�??
	private int currentItem = 0; // 当前页面
	private List<Bitmap> bitmaps;
	private Bitmap bitmap;
	private boolean pdliulan=false;
	private boolean  delete=false;
	private  Animation animation;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_fenye);

		bitmaps = Controller.getInstance().getBitmaps();
		adapter = new ImageAdapter(this, bitmaps);
		fenye_activity_bj=(RelativeLayout) findViewById(R.id.fenye_activity_bj);
		liulan_btu = (ImageView) findViewById(R.id.liulan_btu);
		liulan_btu.setOnClickListener(click);
		jiaye_btu = (ImageView) findViewById(R.id.jiaye_btu);
		jiaye_btu.setOnClickListener(click);
		back_btu = (ImageView) findViewById(R.id.back_btu);
		back_btu.setOnClickListener(click);
		fenye_edit_btu = (ImageView) findViewById(R.id.fenye_edit_btu);

		fenye_edit_btu.setOnClickListener(click);
		group = (ViewGroup) findViewById(R.id.viewGroup);

		fenye_GALLERY = (Gallery) findViewById(R.id.fenye_GALLERY);
		fenye_GALLERY.setAdapter(adapter);
		fenye_GALLERY.setSelection(bitmaps.size()-1);
/*
		tips = new ImageView[bitmaps.size()];
		for (int i = 0; i < tips.length; i++) {
			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(new LayoutParams(10,
					LayoutParams.WRAP_CONTENT));
			tips[i] = imageView;
			if (i == 0) {
				tips[i].setBackgroundResource(R.drawable.fenye__xian_select);
			} else {
				tips[i].setBackgroundResource(R.drawable.fenye__xian_normal);
			}
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					new ViewGroup.LayoutParams(700 / tips.length,
							LayoutParams.WRAP_CONTENT));
			layoutParams.leftMargin = 5;
			layoutParams.rightMargin = 5;
			group.addView(imageView, layoutParams);
		
		}*/
		tips=new ArrayList<ImageView>();
		for(int i = 0; i < bitmaps.size(); i++){
			ImageView imageView = new ImageView(this);
			imageView.setLayoutParams(new LayoutParams(10,
					LayoutParams.WRAP_CONTENT));
			 tips.add(imageView);
			if (i == 0) {
				tips.get(i).setBackgroundResource(R.drawable.fenye__xian_select);
			} else {
				tips.get(i).setBackgroundResource(R.drawable.fenye__xian_normal);
			}
			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					new ViewGroup.LayoutParams(400 / bitmaps.size(),
							LayoutParams.WRAP_CONTENT));
			layoutParams.leftMargin = 5;
			layoutParams.rightMargin = 5;
			group.addView(imageView, layoutParams);
		}
		
		fenye_GALLERY.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> parent, View view,
					final int position, long id) {
				final View jiemain= view;
				
					if (position == oldPosition) {
						tips.get(position).setBackgroundResource(R.drawable.fenye__xian_select);
					} else {
						tips.get(position).setBackgroundResource(R.drawable.fenye__xian_select);
						System.out.println("position==else"+oldPosition);
						tips.get(oldPosition).setBackgroundResource(R.drawable.fenye__xian_normal);
						
					}				
					oldPosition = position;				
					final ImageView image =(ImageView) jiemain.findViewById(R.id.fenye_delect_icon);
					image.setOnClickListener(new OnClickListener() {
						
						@Override
						public void onClick(View v) {							
							if(bitmaps.size()>1){
								
								//有bug--------------------错误
								 group.removeViewAt(position);
								bitmaps.remove(position); 
								adapter.notifyDataSetChanged(); 
							}else{
								group.removeViewAt(position);
								bitmaps.remove(position); 
								adapter.notifyDataSetChanged(); 
								finish();
							}
							Animation animation=AnimationUtils.loadAnimation(getApplicationContext(), R.anim.footer_appear);
							jiemain.startAnimation(animation);	  
								
						}
					});	 					
				
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}
		});	
	}

	@Override
	protected void onStart() {
		bitmaps = Controller.getInstance().getBitmaps();
		if (bitmaps.size() <= 1)
			group.setVisibility(View.INVISIBLE);
		super.onStart();
	}
	@Override
	protected void onStop() {
		bitmaps = Controller.getInstance().getBitmaps();
		if (bitmaps.size() > 0) {
			bitmaps.remove(bitmaps.get(bitmaps.size() - 1));
		}
		super.onStop();
	}

	private View.OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.liulan_btu://切换背景
				if(pdliulan){
					fenye_activity_bj.setBackgroundColor(getResources().getColor(R.color.bowen1));
					pdliulan=false;
				}else{
					
					fenye_activity_bj.setBackgroundColor(getResources().getColor(R.color.bowen));
					pdliulan=true;
				}
				
				break;
			case R.id.jiaye_btu:				
				bitmap = bitmaps.get(bitmaps.size() - 1);
				Controller.getInstance().getBitmaps().add(bitmap);
				finish();		
				break;
			case R.id.back_btu:
				finish();
				break;
			case R.id.fenye_edit_btu:
				showPopupWindow(v);							
				break;
			default:
				break;
			}

		}
	};
	private void showPopupWindow(View view) {

        // �??个自定义的布�??，作为显示的内容
        View contentView = LayoutInflater.from(this).inflate(
                R.layout.fenye_edit_item, null);      
        TextView fenye_allclose_btu=(TextView) contentView.findViewById(R.id.fenye_allclose_btu);
        fenye_allclose_btu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				 bitmaps.clear();
				 group.removeAllViews();
				 bitmaps=null;				
				 adapter.notifyDataSetChanged(); 				 
				 finish();
			}
		});
        TextView fenye_qiehuan_btu=(TextView) contentView.findViewById(R.id.fenye_qiehuan_btu);
        fenye_qiehuan_btu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(getApplicationContext(), "切换至列表式视图", 0).show();			
			}
		});
        final   PopupWindow popupWindow = new PopupWindow(contentView,
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);

        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {          

                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截�?? PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }		
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.drawable.fenye_edit_item_bj));
        // 设置好参数之后再show
        popupWindow.showAtLocation(view, Gravity.TOP, 180, 180);
    }
}
